package com.firemoss.atonement.observation
{
	import org.as3commons.lang.ClassUtils;
	import org.as3commons.reflect.Accessor;
	import org.as3commons.reflect.Field;
	import org.as3commons.reflect.MetaData;
	import org.as3commons.reflect.MetaDataArgument;
	import org.as3commons.reflect.MetadataUtils;
	import org.as3commons.reflect.Type;
	import org.as3commons.reflect.Variable;

	public class ObjectInspector implements IObjectInspector
	{
		/**
		 * Inspects a class for [Bindable] metadata, creating a list of unique event names
		 * to which an ObjectObserver must subscribe to watch changes.
		 */ 
		public function inspect( instance : Object ) : Array
		{
			var type :Type = Type.forInstance( instance );
			var accessors : Array = type.accessors
			var fields : Array = type.fields
			var result : Array = []
				
			for each ( var field : Field in fields )
			{
				for each ( var md : MetaData in field.metaData )
				{
					if ( md.name == "Bindable" )
					{
						for each ( var arg : MetaDataArgument in md.arguments )
						{
							if ( arg.key == "event" && result.indexOf( arg.value ) == -1 )
							{
								result.push( arg.value );
							}
						}
					}
				}
			}
			
			return result;
		}
	}
}